import pymysql
from warnings import filterwarnings
filterwarnings("ignore",category=pymysql.Warning)

# 构建SQL
def build_sql(e):
    sql = "CREATE TABLE IF NOT EXISTS `{0}` (".format(e.table[1])
    for i in range(len(e.field)):
        if e.field[i] != "" and e.field[i] is not None:
            if i == 0:
                sql += "`{0}` {1} AUTO_INCREMENT NOT NULL COMMENT '{2}',".format(e.field[i].replace(" ",""),e.fieldType[i].replace(" ",""),e.fieldName[i].replace(" ",""))
            else:
                sql += "`{0}` {1} COMMENT '{2}',".format(e.field[i].replace(" ",""), e.fieldType[i].replace(" ",""), e.fieldName[i].replace(" ",""))
    sql += "PRIMARY KEY (`{0}`) )COMMENT='{1}'ENGINE=InnoDB DEFAULT CHARSET=utf8;".format(e.field[0].replace(" ",""),e.table[0].replace(" ",""))
    return sql

# 连接mysql/操作数据库
def operateMYSQL(sql,cdb):
    conn = pymysql.connect(host=cdb.url, port=int(cdb.port), user=cdb.username, passwd=cdb.password, db=cdb.db, charset='utf8')
    cur = conn.cursor()
    cur.execute(sql)
    conn.commit()
    cur.close()
    conn.close()
    print("执行SQL：" + sql)